package com.wcs.exam.controller.admin;

import com.wcs.exam.common.base.BaseController;
import com.wcs.exam.common.base.Result;
import com.wcs.exam.common.base.annotation.OptLog;
import com.wcs.exam.common.base.annotation.OptLogCache;
import com.wcs.exam.common.base.page.Page;
import com.wcs.exam.common.llm.bean.AiQuestion;
import com.wcs.exam.controller.admin.biz.AdminQuestionBiz;
import com.wcs.exam.controller.admin.req.*;
import com.wcs.exam.controller.admin.resp.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.util.Collection;
import java.util.List;

/**
 * Admin-试题信息
 *
 * @author wcs
 * @since 2025-03-11
 */
@Tag(name = "Admin-试题信息")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/admin/question")
public class AdminQuestionController extends BaseController {

    @NotNull
    private final AdminQuestionBiz biz;

    @Operation(summary = "试题信息-分页")
    @OptLog(value = "试题信息-分页")
    @PostMapping(value = "/page")
    public Result<Page<AdminQuestionPageResp>> page(@RequestBody @Valid AdminQuestionPageReq req) {
        return biz.page(req);
    }

    @Operation(summary = "试题信息-添加")
    @OptLog(value = "试题信息添加")
    @PostMapping(value = "/save")
    public Result<String> save(@RequestBody @Valid AdminQuestionSaveReq req) {
        return biz.save(req);
    }

    @Operation(summary = "试题信息-批量添加")
    @OptLog(value = "试题信息批量添加")
    @PostMapping(value = "/batch/save")
    public Result<String> batchSave(@RequestBody List<AdminQuestionSaveReq> req) {
        return biz.batchSave(req);
    }

    @Operation(summary = "试题信息-查看")
    @Parameter(name = "id", description = "主键ID", required = true)
    @OptLogCache
    @GetMapping(value = "/view")
    public Result<AdminQuestionViewResp> view(@RequestParam Long id) {
        return biz.view(id);
    }

    @Operation(summary = "试题信息-编辑")
    @OptLog(value = "试题信息编辑", isUpdate = true)
    @PutMapping(value = "/edit")
    public Result<String> edit(@RequestBody @Valid AdminQuestionEditReq req) {
        return biz.edit(req);
    }

    @Operation(summary = "试题信息-排序")
    @OptLog(value = "试题信息-排序")
    @PutMapping(value = "/sort")
    public Result<String> sort(@RequestBody @Valid List<AdminQuestionSortReq> reqs) {
        return biz.sort(reqs);
    }

    @Operation(summary = "试题信息-删除")
    @Parameter(name = "id", description = "主键ID", required = true)
    @OptLog(value = "试题信息删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam Long id) {
        return biz.delete(id);
    }

    @Operation(summary = "试题信息-批量删除")
    @Parameter(name = "ids", description = "主键ID", required = true)
    @OptLog(value = "试题信息批量删除")
    @DeleteMapping(value = "/delete/batch")
    public Result<String> deleteBatch(@RequestBody Collection<Long> ids) {
        return biz.deleteBatch(ids);
    }

    @Operation(summary = "试题信息-移动")
    @OptLog(value = "试题信息移动")
    @PutMapping(value = "/move")
    public Result<String> move(@RequestBody @Valid AdminQuestionMoveReq req) {
        return biz.move(req);
    }

    @Operation(summary = "试题信息-根据条件随机返回试题类型统计集合")
    @PostMapping(value = "/type/stat")
    public Result<List<AdminQuestionTypeCountResp>> typeCount(@RequestBody AdminQuestionTypeCountReq req) {
        return biz.typeCount(req);
    }

    @Operation(summary = "试题信息-根据条件随机返回试题集合")
    @PostMapping(value = "/random")
    public Result<List<AdminQuestionRandomSelectionResp>> randomSelection(@RequestBody AdminQuestionRandomSelectionReq req) {
        return biz.randomSelection(req);
    }

    @Operation(summary = "试题信息-上传解析Excel文件")
    @Parameters({
            @Parameter(name = "file", description = "Excel文件(文件类型仅支持.xls或.xlsx)", in = ParameterIn.QUERY, required = true)
    })
    @PostMapping(value = "/upload/parse/excel")
    public Result<AdminQuestionUploadParseResp> uploadParseExcel(@RequestParam(name = "file") MultipartFile file) {
        return biz.uploadParseExcel(file);
    }

    @Operation(summary = "试题信息-导入试题（Excel）")
    @PostMapping(value = "/import/question")
    public Result<String> importQuestion(@RequestBody AdminQuestionUploadExcelReq req) {
        return biz.importQuestion(req);
    }

    @Operation(summary = "试题信息-引用列表")
    @Parameter(name = "id", description = "主键ID", required = true)
    @GetMapping(value = "/cite/list")
    public Result<List<AdminQuestionCiteListResp>> citeList(@RequestParam Long id) {
        return biz.citeList(id);
    }

    @Operation(summary = "AI智能出题")
    @OptLog(value = "AI智能出题-预提交")
    @PostMapping(value = "/ai/create")
    public Result<String> create(@RequestBody AiQuestion req) {
        return biz.create(req);
    }

    @Operation(summary = "AI智能出题")
    @OptLog(value = "AI智能出题-题目生成")
    @GetMapping(value = "/ai/completions")
    public ResponseBodyEmitter completions(@RequestParam String aiToken) {
        setNoCache();
        return biz.completions(aiToken);
    }
}
